package cn.wolfcode.rbac.web.interceptor;

import cn.wolfcode.rbac.util.UserContext;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    // 前置拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从 session 中获取登录用户
        // Object employee = request.getSession().getAttribute("EMPLOYEE_IN_SESSION");
        Object employee = UserContext.getCurrentUser();
        // 若用户登录过，让其访问
        if(employee != null){
            return true;
        }
        // 若没有登录过，滚回登录页面
        response.sendRedirect("/login.html");
        return false;
    }

}
